# Load Packages -----------------------------------------------------------
library(semTable)

# Run CFAs ----------------------------------------------------------------
setwd("./Code/")
source("./appendix/appendix4_study3_cfas.R")
source("./appendix/appendix4_study4_cfas.R")

# Anti-Dem ----------------------------------------------------------------
# grep("antidemoc_", names(bov), value = T)
antidem_labs <- c("antidemoc_handout" = "Can't Trust because Handouts",
                  "antidemoc_force" = "Traditional Way of Life",
                  "antidemoc_patriotic" = "Patriotic Americans",  
                  "antidemoc_bendrules" = "Leader Bends Rules",
                  "antidemoc_debate" = "Debate Good",
                  "antidemoc_acceptelex" = "Can Accept Elections",
                  "antidemoc_roomtradits" = "Room for Traditions",
                  "antidemoc_leaderlaws"  = "Leader Obeys Laws")
antidem_labs <- stringr::str_to_title(antidem_labs)
names(antidem_labs) <- c("antidemoc_handout",
                         "antidemoc_force",
                         "antidemoc_patriotic",
                         "antidemoc_bendrules",
                         "antidemoc_debate",
                         "antidemoc_acceptelex",
                         "antidemoc_roomtradits",
                         "antidemoc_leaderlaws")

antidem_labs <- c(antidem_labs, antidem = "Anti-Democratic Attitudes")

antidem_tab <- semTable(list("Study 4" = out_s4_antidem, "Study 4, with Method" = out_s4_antidem_method), 
                    columns = c("estsestars"), 
                    columnLabels = c(estsestars = "Estimate (SE)"),
                    paramSets = c("loadings"), 
                    fits = c("chisq", "cfi", "srmr", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper"),
                    fitLabels = c(chisq = "chisq", cfi = "CFI", rmsea = "RMSEA",
                                  rmsea.ci.lower = "RMSEA Lower 90% CI",
                                  rmsea.ci.upper = "RMSEA Upper 90% CI"),
                    file = "antidem.html", 
                    varLabels = antidem_labs, 
                    type = "html", 
                    caption = "Anti-Democratic Attitudes")



# Conspiracy --------------------------------------------------------------
consp_labs <- c("consp_plots" = "Secret Plots",
                "consp_fewppl" = "Few Run Country",
                "consp_dkrun" = "Unknown Control",
                "consp_wars" = "Wars from Small Groups",
                "consp_schoolexps" = "School Explanations OK",
                "consp_complex" = "Events too Complex",
                "consp_democ"  = "Democracies Allow Influence",    
                "consp_US" = "Few People Can't Control US",
                "consp_fewsects" = "Few Big Secrets",
                "consp_democWill" = "Democracy Reflects People")
consp_labs <- stringr::str_to_title(consp_labs)
names(consp_labs) <- c("consp_plots",
                       "consp_fewppl",
                       "consp_dkrun",
                       "consp_wars",
                       "consp_schoolexps",
                       "consp_complex",
                       "consp_democ",
                       "consp_US",
                       "consp_fewsects",
                       "consp_democWill")

consp_labs <- c(consp_labs, consp = "Conspiratorial Predispositions")

consp_tab <- semTable(list("Study 3" = out_s3_consp, "Study 3, with Method" = out_s3_consp_method,
                         "Study 4" = out_s4_consp, "Study 4, with Method" = out_s4_consp_method), 
                    columns = c("estsestars"), 
                    columnLabels = c(estsestars = "Estimate (SE)"),
                    paramSets = c("loadings"), 
                    fits = c("chisq", "cfi", "srmr", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper"),
                    fitLabels = c(chisq = "chisq", cfi = "CFI", rmsea = "RMSEA",
                                  rmsea.ci.lower = "RMSEA Lower 90% CI",
                                  rmsea.ci.upper = "RMSEA Upper 90% CI"),
                    file = "consp.html", 
                    varLabels = consp_labs, 
                    type = "html", 
                    caption = "Conspiratorial Predispositions")

# Hostile Sexism ----------------------------------------------------------
hs_labs <- c("hs_control" = "Control Men",
             "hs_exaggerate" = "Exaggerate Problems",   
             "hs_leash" = "Tight Leash",
             "hs_reasonable" = "Reasonable Demands",
             "hs_feministpower" = "Feminists Don't Want Power",
             "hs_fewwomen" = "Few Tease Men")
hs_labs <- stringr::str_to_title(hs_labs)
names(hs_labs) <- c("hs_control",
                    "hs_exaggerate",
                    "hs_leash",
                    "hs_reasonable",
                    "hs_feministpower",
                    "hs_fewwomen")

hs_labs <- c(hs_labs, hs = "Hostile Sexism")

hs_tab <- semTable(list("Study 3" = out_s3_hs, "Study 3, with Method" = out_s3_hs_method,
                         "Study 4" = out_s4_hs, "Study 4, with Method" = out_s4_hs_method), 
                    columns = c("estsestars"), 
                    columnLabels = c(estsestars = "Estimate (SE)"),
                    paramSets = c("loadings"), 
                    fits = c("chisq", "cfi", "srmr", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper"),
                    fitLabels = c(chisq = "chisq", cfi = "CFI", rmsea = "RMSEA",
                                  rmsea.ci.lower = "RMSEA Lower 90% CI",
                                  rmsea.ci.upper = "RMSEA Upper 90% CI"),
                    file = "hs.html", 
                    varLabels = hs_labs, 
                    type = "html", 
                    caption = "Hostile Sexism")


# Need for Chaos ----------------------------------------------------------
nfc_labs <- c("nfc_teardown" = "tear down",
              "nfc_needchaos" = "need chaos",
              "nfc_destroy" = "destroy",
              "nfc_upholdorder" = "uphold order",
              "nfc_workinside" = "work inside",
              "nfc_protectinstits" = "protect institutions", 
              "nfc_greatthings" = "great things",
              "nfc_disastFun" = "disasters are fun",     
              "nfc_disastRebuild" = "disaster Rebuild",
              "nfc_burnsociety" = "burn society",
              "nfc_burninstits" = "burn institutions",
              "nfc_clearrules" = "clear rules",    
              "nfc_disastfear" = "disaster fear",
              "nfc_chaosupset" = "chaos upset",
              "nfc_respectproduct"  = "respect product",
              "nfc_disastForeign" = "disaster foreign county")
nfc_labs <- stringr::str_to_title(nfc_labs)
names(nfc_labs) <- c("nfc_teardown",
                     "nfc_needchaos",
                     "nfc_destroy",
                     "nfc_upholdorder",
                     "nfc_workinside",
                     "nfc_protectinstits",
                     "nfc_greatthings",
                     "nfc_disastFun",
                     "nfc_disastRebuild",
                     "nfc_burnsociety",
                     "nfc_burninstits",
                     "nfc_clearrules",
                     "nfc_disastfear",
                     "nfc_chaosupset",
                     "nfc_respectproduct",
                     "nfc_disastForeign")

nfc_labs <- c(nfc_labs, nfc = "Need for Chaos")

nfc_tab <- semTable(list("Study 3" = out_s3_nfc, "Study 3, with Method" = out_s3_nfc_method,
                         "Study 4" = out_s4_nfc, "Study 4, with Method" = out_s4_nfc_method), 
                    columns = c("estsestars"), 
                    columnLabels = c(estsestars = "Estimate (SE)"),
                    paramSets = c("loadings"), 
                    fits = c("chisq", "cfi", "srmr", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper"),
                    fitLabels = c(chisq = "chisq", cfi = "CFI", rmsea = "RMSEA",
                                  rmsea.ci.lower = "RMSEA Lower 90% CI",
                                  rmsea.ci.upper = "RMSEA Upper 90% CI"),
                    file = "nfc.html", 
                    varLabels = nfc_labs, 
                    type = "html", 
                    caption = "Need for Chaos")

# Political Violence ------------------------------------------------------
viol_labs <- c("viol_threatpols" = "Threaten Politicians",
              "viol_bricks" = "Throw Bricks",
              "viol_stopbadgovt" = "Stop Gov't with Physical Force",
              "viol_bullets" = "Bullets can Fix",     
              "viol_noviol" = "Never use Violence",
              "viol_nonviolprot" = "Protest always Non-Violent",
              "viol_violunaccept" = "Violence Unacceptable",
              "viol_notit4tat" = "No Violent Responses")
viol_labs <- stringr::str_to_title(viol_labs)
names(viol_labs) <- c("viol_threatpols",
                     "viol_bricks",
                     "viol_stopbadgovt",
                     "viol_bullets",
                     "viol_noviol",
                     "viol_nonviolprot",
                     "viol_violunaccept",
                     "viol_notit4tat")

viol_labs <- c(viol_labs, viol = "Political Violence")

viol_tab <- semTable(list("Study 3" = out_s3_viol, "Study 3, with Method" = out_s3_viol_method,
                         "Study 4" = out_s4_viol, "Study 4, with Method" = out_s4_viol_method), 
                    columns = c("estsestars"), 
                    columnLabels = c(estsestars = "Estimate (SE)"),
                    paramSets = c("loadings"), 
                    fits = c("chisq", "cfi", "srmr", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper"),
                    fitLabels = c(chisq = "chisq", cfi = "CFI", rmsea = "RMSEA",
                                  rmsea.ci.lower = "RMSEA Lower 90% CI",
                                  rmsea.ci.upper = "RMSEA Upper 90% CI"),
                    file = "viol.html", 
                    varLabels = viol_labs, 
                    type = "html", 
                    caption = "Political Violence")


# Populism ----------------------------------------------------------------
pop_labs <- c("pop_fewints" = "Few Big Interests",
              "pop_crooked" = "Politicians Crooked",
              "pop_nomethink" = "Politicians Dont't Care",
              "pop_polsimprove" = "Officials Improve Lives",
              "pop_yesmethink" = "Politicians Interested", 
              "pop_benefitall"  = "Benefits People")
pop_labs <- stringr::str_to_title(pop_labs)
names(pop_labs) <- c("pop_fewints",
                     "pop_crooked",
                     "pop_nomethink",
                     "pop_polsimprove",
                     "pop_yesmethink",
                     "pop_benefitall")

pop_labs <- c(pop_labs, pop = "Populism")

pop_tab <- semTable(list("Study 3" = out_s3_pop, "Study 3, with Method" = out_s3_pop_method,
                         "Study 4" = out_s4_pop, "Study 4, with Method" = out_s4_pop_method), 
                    columns = c("estsestars"), 
                    columnLabels = c(estsestars = "Estimate (SE)"),
                    paramSets = c("loadings"), 
                    fits = c("chisq", "cfi", "srmr", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper"),
                    fitLabels = c(chisq = "chisq", cfi = "CFI", rmsea = "RMSEA",
                                  rmsea.ci.lower = "RMSEA Lower 90% CI",
                                  rmsea.ci.upper = "RMSEA Upper 90% CI"),
                    file = "pop.html", 
                    varLabels = pop_labs, 
                    type = "html", 
                    caption = "Populism")



# RR ----------------------------------------------------------------------
rr_labs <- c("rr_specfavr" = "Special Favors",
             "rr_thard" = "Try Hard",
             "rr_dless" = "Deserve",
             "rr_pdisc" = "Past Discrimination")
rr_labs <- stringr::str_to_title(rr_labs)
names(rr_labs) <- c("rr_specfavr",
                    "rr_thard",
                    "rr_dless",
                    "rr_pdisc")

rr_labs <- c(rr_labs, rr = "Racial Resentment")

rr_tab <- semTable(list("Study 3" = out_s3_rr, "Study 4" = out_s4_rr), 
                    columns = c("estsestars"), 
                    columnLabels = c(estsestars = "Estimate (SE)"),
                    paramSets = c("loadings"), 
                    fits = c("chisq", "cfi", "srmr", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper"),
                    fitLabels = c(chisq = "chisq", cfi = "CFI", rmsea = "RMSEA",
                                  rmsea.ci.lower = "RMSEA Lower 90% CI",
                                  rmsea.ci.upper = "RMSEA Upper 90% CI"),
                    file = "rr.html", 
                    varLabels = rr_labs, 
                    type = "html", 
                    caption = "Racial Resentment")
